package com.github_dididipapa.mall.product.app;

import java.util.Arrays;
import java.util.Map;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.github_dididipapa.common.validator.group.AddGroup;
import com.github_dididipapa.common.validator.group.UpdateGroup;
import com.github_dididipapa.common.validator.group.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.github_dididipapa.mall.product.entity.BrandEntity;
import com.github_dididipapa.mall.product.service.BrandService;
import com.github_dididipapa.common.utils.PageUtils;
import com.github_dididipapa.common.utils.R;


/**
 * 品牌
 *
 * @author dididipapa
 * @email not_found@404.com
 * @date 2025-04-26 18:51:12
 */
@Validated
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R listBrand(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R infoBrand(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping( "/save")
    //@RequiresPermissions("product:brand:save")
    public R saveBrand(@RequestBody @Validated(AddGroup.class) BrandEntity brand){
//        if (result.hasErrors()) {
//            Map<String, String> map = new HashMap<>();
//            // 获取校验的错误结果
//            result.getFieldErrors().forEach((item) -> {
//                // 获取到错误提示FieldError
//                String message = item.getDefaultMessage();
//                // 获取错误的属性名字
//                String field = item.getField();
//                map.put(field, message);
//            });
//            return R.ok().error(400, "提交的数据不合法").put("data", map);
//        }else {
            brandService.save(brand);
            return R.ok();
//        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R updateBrand(@RequestBody @Validated(UpdateGroup.class) BrandEntity brand){
		brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 修改状态
     */
    @RequestMapping("/update/status")
    //@RequiresPermissions("product:brand:update")
    public R updateBrandForStatus(@RequestBody @Validated(UpdateStatusGroup.class) BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R deleteBrand(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
